package com.zui.util.excel;

import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;

public class CreateExcelImpl implements CreateExcel {

	private HSSFWorkbook wb = null;
	private HSSFSheet sheet = null;
	private HSSFRow row = null;
	private HSSFCell cell = null;
	private ExcelInfoImpl excelInfo = null;
	private int rowNum = 0; //当前需要创建的行数，初始化为0
	
	/**
	 * 初始化HSSFWorkbook，并默认创建一个sheet
	 * @param className
	 * 			类的全路径
	 */
	public CreateExcelImpl(String className,List<String> fieldsName){
		wb = new HSSFWorkbook();
		createSheet("sheet1");
		excelInfo = new ExcelInfoImpl(className,fieldsName);
	}
	
	/**
	 * 创建一个单元格
	 * @param cellNum
	 * 			单元格所属位置
	 */
	public void createCell(int cellNum) {
		cell = row.createCell((short)cellNum);

	}

	/**
	 * 创建一行，并把当前行加1	
	 */
	public void createRow() {
		row = sheet.createRow(rowNum);
		rowNum++;
	}

	/**
	 * 创建一个sheet
	 * @param sheetName
	 * 			sheet名字
	 */
	public void createSheet(String sheetName) {
		sheet = wb.createSheet(sheetName);

	}

	/**
	 * 设置一个单元格内容
	 * @param data
	 * 			单元格内容
	 * @param cellNum
	 * 			单元格所属位置
	 */
	public void setCell(String data, int cellNum) {
		createCell(cellNum);
		cell.setCellValue(data);
	}

	/**
	 * 设置单元格内容
	 * @param datas
	 * 			单元格数据
	 * @return 当前对象
	 */
	public CreateExcelImpl setCellData(List<?> datas) throws Exception{
		List<String> list = excelInfo.getPropertys();
		
		for(int j = 0; j < datas.size(); j++){
			Object obj = datas.get(j);
			createRow();
			for(int i = 0; i < list.size(); i++){
				setCell(excelInfo.getContent(obj, list.get(i)),i);
			}
		}
		return this;
	}

	/**
	 * 设置表头内容
	 * @return 当前对象
	 */
	public CreateExcelImpl setHead() {
		List<String> datas = excelInfo.getPropertyNames();
		if(datas.size() == 0){
			datas = excelInfo.getHeaders();
		}
		createRow();
		for(int i = 0; i < datas.size(); i++){
			setCell((String)datas.get(i), i);
		}
		return this;
	}

	/**
 	 * 设置标题内容
 	 * @param title
 	 * 			标题
 	 * @return 当前对象
 	 */
	public CreateExcelImpl setTitle(String title) {
		this.createRow();
		this.createCell(0);
		
		HSSFFont titlefont = wb.createFont();
		titlefont.setFontName("Arial");
		titlefont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		titlefont.setFontHeight((short)400);
		HSSFCellStyle titleStyle = wb.createCellStyle();
		titleStyle.setFont(titlefont);
		titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		cell.setCellStyle(titleStyle);
		
		cell.setCellValue(title);
		sheet.addMergedRegion(new Region(0, (short)0, 1, (short)(excelInfo.getNum()-1)));
		rowNum++;
		return this;
	}

	/**
	 * 获取HSSFWorkbook
	 * @return HSSFWorkbook
	 */
	public HSSFWorkbook getWb() {
		return wb;
	}

}
